// pages/substitute-class/list/list.js
Page({
  data: {
    classList: [],
    loading: false,
    hasMore: true,
    page: 1,
    pageSize: 10,
    filterType: 'all', // all, urgent, high-pay
    searchKeyword: ''
  },

  onLoad() {
    this.loadClassList()
  },

  onPullDownRefresh() {
    this.setData({
      page: 1,
      classList: [],
      hasMore: true
    })
    this.loadClassList()
    wx.stopPullDownRefresh()
  },

  onReachBottom() {
    if (this.data.hasMore && !this.data.loading) {
      this.loadClassList()
    }
  },

  loadClassList() {
    if (this.data.loading) return
    
    this.setData({ loading: true })
    
    // 模拟API调用
    setTimeout(() => {
      const mockData = this.generateMockData()
      this.setData({
        classList: this.data.page === 1 ? mockData : [...this.data.classList, ...mockData],
        loading: false,
        hasMore: mockData.length === this.data.pageSize,
        page: this.data.page + 1
      })
    }, 1000)
  },

  generateMockData() {
    const courses = ['高等数学', '大学英语', '计算机基础', '线性代数', '概率论', '数据结构']
    const locations = ['教学楼A101', '教学楼B203', '实验楼C301', '图书馆报告厅']
    const teachers = ['张教授', '李老师', '王副教授', '刘讲师']
    
    return Array.from({ length: this.data.pageSize }, (_, i) => ({
      id: Date.now() + i,
      courseName: courses[Math.floor(Math.random() * courses.length)],
      teacher: teachers[Math.floor(Math.random() * teachers.length)],
      location: locations[Math.floor(Math.random() * locations.length)],
      time: '2024-01-' + (15 + Math.floor(Math.random() * 10)) + ' 14:00-16:00',
      reward: Math.floor(Math.random() * 50) + 20,
      urgent: Math.random() > 0.7,
      description: '因有急事无法上课，寻找代课同学，需要认真听讲并做好笔记',
      publisher: '同学' + Math.floor(Math.random() * 1000),
      publishTime: '2小时前',
      status: 'pending' // pending, accepted, completed
    }))
  },

  onFilterChange(e) {
    const filterType = e.currentTarget.dataset.type
    this.setData({
      filterType,
      page: 1,
      classList: [],
      hasMore: true
    })
    this.loadClassList()
  },

  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    })
  },

  onSearch() {
    this.setData({
      page: 1,
      classList: [],
      hasMore: true
    })
    this.loadClassList()
  },

  navigateToDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/substitute-class/detail/detail?id=${id}`
    })
  },

  navigateToPublish() {
    wx.navigateTo({
      url: '/pages/substitute-class/publish/publish'
    })
  }
})